﻿function Bonus(parentContainer, countContainer, templateContainer, newMeberIDPrefix, removeMemberIDPrefix, noPrefix, ID, userID) {
    this.ParentContainer = parentContainer;
    this.CountContainer = countContainer;
    this.TemplateContainer = templateContainer;
    this.TemplateContainerHtmlContent = templateContainer.innerHTML;
    this.Items = new Array();
    this.NewMemberID = newMeberIDPrefix;
    this.RemoveMemberID = removeMemberIDPrefix;
    this.NoPrefix = noPrefix;
    this.ID = ID;
    this.UserID = userID;
}

Bonus.prototype.addMember = function (empID, empName, depName, month,amount) {
    if (this.Items.contains(empID)) {
        return false;
    }
    var count = parseInt(this.CountContainer.value) + 1;
    this.CountContainer.value = count;

    var templateContent = new String(this.TemplateContainerHtmlContent);
    templateContent = templateContent.replace(/_0/gi, "_" + count);
    templateContent = templateContent.replace(/_count/gi, count);
    templateContent = templateContent.replace(/MemberNo/gi, count);
    templateContent = templateContent.replace(/EmpID/gi, decodeURIComponent(empID));
    templateContent = templateContent.replace(/EmpName/gi, decodeURIComponent(empName));
    templateContent = templateContent.replace(/DepName/gi, decodeURIComponent(depName));
    templateContent = templateContent.replace(/Month/gi, decodeURIComponent(month));
    templateContent = templateContent.replace(/Amount/gi, decodeURIComponent(amount));
    var newContainer = document.createElement("DIV");
    newContainer.id = this.NewMemberID + count;
    newContainer.innerHTML = templateContent;
    this.ParentContainer.appendChild(newContainer);

    setRemoveControl(document.getElementById(this.NewMemberID + count), true);

    this.Items.push(empID);
    reOrder(parseInt(this.CountContainer.value), this.ParentContainer, this.NewMemberID, this.NoPrefix);

    return true;
}

Bonus.prototype.removeMember = function (no, empID) {
    var allContainers = this.ParentContainer.getElementsByTagName("DIV");
    for (var i = 0; i < allContainers.length; i++) {
        if (allContainers[i].id == this.NewMemberID + no) {
            this.ParentContainer.removeChild(allContainers[i]);
            this.Items.remove(empID);
            break;
        }
    }
    var count = parseInt(this.CountContainer.value);
    count--;
    this.CountContainer.value = count;
    reOrder(parseInt(this.CountContainer.value), this.ParentContainer, this.NewMemberID, this.NoPrefix);

    var postData =
    {
        "ProjectID": encodeURIComponent(this.ID),
        "EmployeeID": encodeURIComponent(empID),
        "LastModifiedBy": encodeURIComponent(this.UserID)
    };
    var jsonStr = $.toJSON(postData);

    $.ajax({
        url: "/Handler/BonusProjectDetail.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "removeData", jsonDataString: jsonStr },
        success: function (data) {

        }
    });
}

function reOrder(count, parentContainer, newMemberID, noPrefix) {
    var allContainers = parentContainer.getElementsByTagName("DIV");
    var cnt = 1;
    for (var i = 0; i < allContainers.length; i++) {
        var id = allContainers[i].id;
        var index = id.toString().indexOf(newMemberID) + newMemberID.length;
        var noID = noPrefix + id.substring(index);
        document.getElementById(noID).value = cnt;
        cnt++;
    }
}

function setRemoveControl(parentContainer, isShow) {
    var removeCtrls = parentContainer.getElementsByTagName("a");
    for (var i = 0; i < removeCtrls.length; i++) {
        if (removeCtrls[i].id.indexOf(this.RemoveMemberID) >= 0) {
            removeCtrls[i].style.display = isShow ? "" : "none";
        }
    }
} 